package org.ss.psci.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmAddressList;

/**
 * Data access object (DAO) for domain model class PmAddressList.
 * 
 * @see org.ss.psci.entity.PmAddressList
 * @author MyEclipse Persistence Tools
 */

public class PmAddressListDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmAddressListDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String COMPANY = "company";
	public static final String PHONE = "phone";
	public static final String TEL = "tel";
	public static final String ADDRESS = "address";
	public static final String MAIL = "mail";
	public static final String SEX = "sex";
	public static final String REMARK = "remark";
	public static final String NICKNAME = "nickname";
	public static final String USERNAME = "username";

	public PmAddressList copyEntity(PmAddressList dest, PmAddressList orig) {
		if (dest == null || orig == null) {
			return new PmAddressList();
		}
		if (orig.getCompany() != null) {
			dest.setCompany(orig.getCompany());
		}
		if (orig.getAddress() != null) {
			dest.setAddress(orig.getAddress());
		}
		if (orig.getMail() != null) {
			dest.setMail(orig.getMail());
		}
		if (orig.getName()!= null) {
			dest.setName(orig.getName());
		}
		if (orig.getNickname()!= null) {
			dest.setNickname(orig.getNickname());
		}
		if (orig.getPhone() != null) {
			dest.setPhone(orig.getPhone());
		}
		if (orig.getRemark() != null) {
			dest.setRemark(orig.getRemark());
		}
		if (orig.getSex() != null) {
			dest.setSex(orig.getSex());
		}
		if (orig.getTel() != null) {
			dest.setTel(orig.getTel());
		}
		if (orig.getUsername() != null) {
			dest.setUsername(orig.getUsername());
		}
		return dest;
	}

	protected void initDao() {
		// do nothing
	}

	public void save(PmAddressList transientInstance) {
		log.debug("saving PmAddressList instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmAddressList persistentInstance) {
		log.debug("deleting PmAddressList instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmAddressList findById(java.lang.Integer id) {
		log.debug("getting PmAddressList instance with id: " + id);
		try {
			PmAddressList instance = (PmAddressList) getHibernateTemplate()
					.get("org.ss.psci.entity.PmAddressList", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmAddressList instance) {
		log.debug("finding PmAddressList instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmAddressList instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from PmAddressList as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByCompany(Object company) {
		return findByProperty(COMPANY, company);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByMail(Object mail) {
		return findByProperty(MAIL, mail);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByNickname(Object nickname) {
		return findByProperty(NICKNAME, nickname);
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findAll() {
		log.debug("finding all PmAddressList instances");
		try {
			String queryString = "from PmAddressList";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmAddressList merge(PmAddressList detachedInstance) {
		log.debug("merging PmAddressList instance");
		try {
			PmAddressList result = (PmAddressList) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmAddressList instance) {
		log.debug("attaching dirty PmAddressList instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmAddressList instance) {
		log.debug("attaching clean PmAddressList instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmAddressListDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (PmAddressListDAO) ctx.getBean("PmAddressListDAO");
	}
}